package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.entity.Register;
import com.sky.entity.User;
import com.sky.mapper.RegisterMapper;
import com.sky.mapper.UserMapper;
import com.sky.result.PageResult;
import com.sky.service.RegisterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RegisterMapper registerMapper;
    @Override
    public String register(Register register1) {
        String username;
        while (true){
            //随机生成一个11位的数字
            username= Long.toString(ThreadLocalRandom.current().nextLong(10000000000L, 100000000000L));

            User user1 = userMapper.getByUsername(username);
            if (user1 != null && Objects.equals(register1.getRole(), user1.getRole())) {
                continue;
            }
            Register register = registerMapper.getByUsername(username);
            if(register!=null && register.getResult()==null){
                continue;
            }
            break;
        }
        register1.setUsername(username);
        registerMapper.insert(register1);
        return username;
    }

    @Override
    public PageResult getNoAudit(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Register> registers = registerMapper.getNoAudit();
        Page<Register> page1=(Page<Register>) registers;
        return new PageResult(page1.getTotal(),page1.getResult());

    }

    @Override
    public PageResult getNoPass(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Register> registers = registerMapper.getNoPass();
        Page<Register> page1=(Page<Register>) registers;
        return new PageResult(page1.getTotal(),page1.getResult());

    }

    @Override
    public PageResult getRegistered(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<User> users = userMapper.getAll();
        Page<User> page1=(Page<User>) users;
        return new PageResult(page1.getTotal(),page1.getResult());

    }

    @Override
    public void deleteNoPassById(Integer id) {
        registerMapper.deleteById(id);
    }

    @Override
    public void rejectRegister(Integer id) {
        registerMapper.rejectRegister(id);
    }

    @Override
    @Transactional
    public void acceptRegister(Integer id) {
        Register register = registerMapper.getById(id);
        User user = new User();
        BeanUtils.copyProperties(register,user);
        user.setStatus("1");
        userMapper.insert(user);
        registerMapper.deleteById(id);
    }
}
